
#include "CXMLNode.h"

CXMLNode::CXMLNode()
{
}

CXMLNode::~CXMLNode()
{
}

CXMLNode CXMLNode::parseXML(QString sFileName)
{
	CXMLNode tNode;

	QFile xmlFile(sFileName);

	if (xmlFile.exists())
	{
		if (xmlFile.open(QIODevice::ReadOnly))
		{
			QDomDocument doc("myDocument");
			doc.setContent(&xmlFile);

			tNode = CXMLNode::parseXMLNode(doc.documentElement());

			xmlFile.close();
		}
	}

	return tNode;
}

CXMLNode CXMLNode::parseXMLNode(QDomNode node)
{
	CXMLNode tNode;

	tNode.m_sTag = node.nodeName();
	tNode.m_sValue	= node.nodeValue();

	for (uint Index = 0; Index < node.attributes().length(); Index++)
	{
		QDomNode attrNode = node.attributes().item(Index);
		tNode.m_vAttributes[attrNode.nodeName()] = attrNode.nodeValue();
	}

	for (uint Index = 0; Index < node.childNodes().length(); Index++)
	{
		tNode.m_vNodes.append(CXMLNode::parseXMLNode(node.childNodes().at(Index)));
	}

	return tNode;
}

CXMLNode CXMLNode::getNodeByTagName(QString sTagName)
{
	foreach (CXMLNode tNode, m_vNodes)
	{
		if (tNode.m_sTag == sTagName) return tNode;
	}

	return CXMLNode();
}

QVector<CXMLNode> CXMLNode::getNodesByTagName(QString sTagName)
{
	QVector<CXMLNode> vNodes;

	foreach (CXMLNode tNode, m_vNodes)
	{
		if (tNode.m_sTag == sTagName)
		{
			vNodes.append(tNode);
		}
	}

	return vNodes;
}
